<template>
    <div class="depTree">
        <el-input placeholder="请输入部门名称" prefix-icon="el-icon-search" v-model="filterText"></el-input>

        <el-tree class="filter-tree"
                 :data="deps"
                 :props="defaultProps"
                 :filter-node-method="filterNode"
                 :expand-on-click-node="false"
                 ref="tree">
             <span class="custom-tree-node" slot-scope="{ data }">
                <span>{{ data.name }}</span>
                <span>
                    <el-button type="primary" size="mini" @click="() => showAddDepView(data)">
                        添加部门
                    </el-button>
                    <el-button type="danger" size="mini" @click="() => deleteDep(data)">
                        删除部门
                    </el-button>
                </span>
            </span>
        </el-tree>

        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <el-tag type="success">上级部门</el-tag>
                        <td style="color: green">{{pname}}</td>
                    </tr>
                    <tr>
                        <td><b>部门名称</b></td>
                        <td>
                            <el-input v-model="dep.name" placeholder="请输入部门名称"></el-input>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="doAddDep">确 定</el-button>
             </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "DepMana",
        data(){
            return{
                filterText:'',
                deps: [],
                defaultProps:{
                    children: 'children',
                    label: 'name'
                },
                dep:{
                    name: '',
                    parentId: -1
                },
                pname: '',
                dialogVisible: false
            }
        },
        watch:{
          filterText(val){
              this.$refs.tree.filter(val);
          }
        },
        mounted() {
            this.initDeps();
        },
        methods:{
            //初始化
            initDeps(){
                this.getRequest('/system/basic/department/').then(resp => {
                    if (resp){
                        this.deps = resp;
                    }
                })
            },
            initDep(){
                this.dep = {
                    name:'',
                    parentId: -1
                }
                this.pname = '';
            },
            //拦截节点
            filterNode(value,data){
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            },
            showAddDepView(data){
                this.dialogVisible = true;
                this.pname = data.name;
                this.dep.parentId = data.id;
            },
            doAddDep(){
                this.postRequest('/system/basic/department/',this.dep).then(resp =>{
                    if (resp){
                        this.dialogVisible = false;
                        this.addObjToDep(this.deps,resp.object);
                        this.initDep();
                    }
                });
            },
            addObjToDep(deps,obj){
                if (deps == null){
                    deps = [];
                }
                for (let i = 0;i < deps.length; i++){
                    let d = deps[i];
                    if (d.id == obj.parentid){
                        d.children = d.children.concat(obj);
                        if (d.children.length > 0 ){
                            d.isparent = true;
                        }
                        return;
                    }else{
                        this.addObjToDep(d.children,obj);
                    }
                }
            },
            deleteDep(data){
                if (data.isparent){
                    this.$message.error('父部门删除失败');
                }else{
                    this.$confirm('此操作将永久删除['+ data.name +']部门, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest('/system/basic/department/' + data.id).then(resp =>{
                            if (resp){
                                this.removeDepFromDeps(null,this.deps,data.id);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
            removeDepFromDeps(p,deps,id){
                if (deps == null){
                    deps = [];
                }
                for (let i = 0; i < deps.length; i++){
                    let d = deps[i];
                    if (d.id == id){
                        deps.splice(i,1);
                        if (deps.length == 0){
                            p.isparent = false;
                        }
                        return;
                    }else{
                        this.removeDepFromDeps(d,d.children,id);
                    }
                }
            }

        }
    }
</script>

<style>
    .depTree{
        width: 500px;
    }
    .custom-tree-node{
        width: 100%;
        display: flex;
        justify-content: space-between;
    }
    .custom-tree-node .el-button{
        padding: 2px;
    }

</style>